package com.link2room.ironhide.controller.manager;

import com.link2room.common.dto.result.base.BaseResultDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.ironhide.dto.param.manager.L2RGroupAreaManagerParamDto;
import com.link2room.ironhide.dto.result.base.L2RGroupAreaChildrenDto;
import com.link2room.ironhide.dto.result.base.L2RGroupAreaDto;
import com.link2room.ironhide.dto.result.base.PositionBaseDto;
import com.link2room.ironhide.facade.IGroupAreaManagerFacadeService;
import com.link2room.ironhide.facade.IL2RPositionsFacadeService;
import com.link2room.sideswipe.permissionResouces.annotation.L2RPermissionResources;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static com.link2room.ironhide.constant.PermissionConstant.*;

/**
 * Created by sss on 2016/10/11.
 */
@Api(tags = "集团区域管理")
@RestController
@RequestMapping("/manager/GroupAreas")
public class GroupAreaManagerController {
    @Resource
    private IGroupAreaManagerFacadeService groupAreaManagerFacadeService;

    @Resource
    private IL2RPositionsFacadeService l2RPositionsFacadeServiceImpl;

    /**
     * 集团城市列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询集团区域")
    @RequestMapping(method = RequestMethod.GET)
    @L2RPermissionResources(value = groupAreas, name = "查询集团区域")
    public BaseResultDto<L2RGroupAreaDto> listL2RGroupArea(
            @RequestParam(name = "currentPage", required = false) Integer currentPage,
            @RequestParam(name = "pageSize", required = false) Integer pageSize
    ) throws Exception {
        L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto = new L2RGroupAreaManagerParamDto();
        l2RGroupAreaManagerParamDto.setCurrentPage(currentPage);
        l2RGroupAreaManagerParamDto.setPageSize(pageSize);
        QueryResult<L2RGroupAreaDto> l2RGroupAreaResult = groupAreaManagerFacadeService.listL2RGroupArea(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setQueryResult(l2RGroupAreaResult);
        return l2rRs;
    }

    /**
     * 集团级联城市列表
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询集团级联城市", response = L2RGroupAreaChildrenDto.class)
    @RequestMapping(value = "/Children", method = RequestMethod.GET)
    @L2RPermissionResources(value = groupAreaChildren, name = "查询集团级联城市")
    public BaseResultDto listL2RGroupAreaChildren() throws Exception {
        L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto = new L2RGroupAreaManagerParamDto();
        List<L2RGroupAreaChildrenDto> l2rGroupAreaChildren = groupAreaManagerFacadeService.listL2RGroupAreaChildren(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2rGroupAreaChildren);
        return l2rRs;
    }

    /**
     * 保存集团城市
     *
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "保存集团城市", response = L2RGroupAreaDto.class)
    @RequestMapping(method = RequestMethod.POST)
    @L2RPermissionResources(value = saveGroupAreas, name = "保存集团城市")
    public BaseResultDto saveL2RGroupArea(@RequestBody L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto
    ) throws Exception {
        L2RGroupAreaDto l2RGroupArea = groupAreaManagerFacadeService.saveL2RGroupArea(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RGroupArea);
        return l2rRs;
    }

    /**
     * 更新集团城市
     *
     * @param l2RGroupAreaManagerParamDto
     * @return
     * @throws Exception
     */
    @ApiOperation(value="更新集团城市", response =L2RGroupAreaDto.class )
    @RequestMapping(method = RequestMethod.PUT)
    @L2RPermissionResources(value = updateGroupAreas, name="更新集团城市")
    public BaseResultDto updateL2RGroupArea(
            @RequestBody L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto
    ) throws Exception {
        L2RGroupAreaDto l2RGroupArea = groupAreaManagerFacadeService.updateL2RGroupArea(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RGroupArea);
        return l2rRs;
    }

    @RequestMapping(value = "/Positions", method = RequestMethod.GET)
    public BaseResultDto Positions(
            @RequestParam(value = "cityCode", required = true) Long cityCode
    ) throws Exception {
        L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto = new L2RGroupAreaManagerParamDto();
        l2RGroupAreaManagerParamDto.setCityCode(cityCode);
        List<PositionBaseDto> l2RPositionBaseList = l2RPositionsFacadeServiceImpl.listPositionBase(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RPositionBaseList);
        return l2rRs;
    }

    @RequestMapping(value = "/GroupPositions", method = RequestMethod.POST)
    @L2RPermissionResources(value = newGroupPositions, name="新建集团位置信息")
    public BaseResultDto newPositions(
            @RequestBody L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto
    ) throws Exception {
        l2RPositionsFacadeServiceImpl.saveL2RGroupPositionBase(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        return l2rRs;
    }

    @ApiOperation(value="获取集团属性列表",response = PositionBaseDto.class)
    @RequestMapping(value = "/GroupPositions",method = RequestMethod.GET)
    @L2RPermissionResources(value = groupPositions,name="获取集团属性列表")
    public BaseResultDto groupPositions(
            @RequestParam(value = "cityCode", required = true) Long cityCode
    ) throws Exception {
        L2RGroupAreaManagerParamDto l2RGroupAreaManagerParamDto = new L2RGroupAreaManagerParamDto();
        l2RGroupAreaManagerParamDto.setCityCode(cityCode);
        List<PositionBaseDto> l2RPositionBaseList = l2RPositionsFacadeServiceImpl.listPositionBaseGroupUse(l2RGroupAreaManagerParamDto);
        BaseResultDto l2rRs = new BaseResultDto();
        l2rRs.setResultCode(0);
        l2rRs.setResultInfo(l2RPositionBaseList);
        return l2rRs;
    }
}
